import React from "react";
import {Select} from "antd";
import BaseComponent from "../../../../redare/components/BaseComponent";
import {ifTrue} from "../../../../redare/utils/Utils";
import PlatformPresenter from "../../.gen/presenter/component/core/PlatformPresenter";
import {isLoginUserInRootPlatform} from "../../utils/WorkbenchUtil";

export default class RoleSelect extends BaseComponent {

    static defaultProps = {
        platform: undefined,
        onValueChange: undefined,
        style: undefined
    };

    constructor(props) {
        super(props);
        if (ifTrue(this.props.value)) {
            if (!(this.props.value instanceof Array)) {
                this.state.value = [this.props.value];
            } else {
                this.state.value = this.props.value;
            }
        }
        this.state.data = [];
        this.presenter = new PlatformPresenter(this);
        this._form.platform = this.props.platform;
    }

    UNSAFE_componentWillReceiveProps({platform, value}, nextContext) {
        if (platform !== this.props.platform) {
            this.setPlatform(platform);
        }
        if (this.props.value !== value) {
            this._onValueChange(value);
        }
    }

    componentDidMount() {
        if (this._form.platform) {
            this._findRoleList();
        }
    }

    setPlatform(platform, unClearValue) {
        if (!unClearValue && this._form.platform !== platform) {
            this._onValueChange();
        }
        this._form.platform = platform;
        if (this._form.platform) {
            this._findRoleList();
        } else {
            this.setState({data: []});
        }
    }

    setValue(value) {
        this._onValueChange(value);
    }

    _findRoleList() {
        let {platform} = this._form;
        if (isLoginUserInRootPlatform()) {
            this.presenter.findRoleList({platform, type: 1}, {returnAll: true}, (result) => {
                this.setState({
                    data: result.data
                })
            });
        } else {
            this.presenter.findRoleList({platform, platformUnionUserPlatform: true}, {returnAll: true}, (result) => {
                this.setState({
                    data: result.data
                })
            });
        }
    }

    _onValueChange(value) {
        if (ifTrue(value)) {
            if (!(value instanceof Array)) {
                value = [value];
            }
        }
        this.setState({value});
        if (this.props.onValueChange) {
            this.props.onValueChange(value);
        }
    }

    renderOption() {
        let options = [];
        this.state.data.forEach((v) => {
            options.push(
                <Select.Option key={v.roleId} value={v.roleId}>{v.name}</Select.Option>
            )
        });
        return options;
    }

    render() {
        return (
            <Select mode="multiple" allowClear={true} value={this.state.value} onChange={this._onValueChange.bind(this)}
                    style={this.props.style}>
                {
                    this.renderOption()
                }
            </Select>
        )
    }
}
